package com.qiao.glod.api;

import com.qiao.glod.application.QApplication;
import com.qiao.glod.bean.ImageWarper;
import com.qiao.glod.bean.Results;

import java.io.File;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
import retrofit2.http.Path;
import retrofit2.http.Query;

/**
 * Created by Qiao on 2016/4/16.
 * function：
 */
public class GoldApi  {

    /**每次加载条目*/
    public static final int LOAD_LIMIT = 20;

    private final GankInterface mGoldinterface;
    private final MeituInterface mMeituInterface;

    public static GoldApi instance;

    public static GoldApi getInstance() {
        if (null == instance) {
            synchronized (GoldApi.class) {
                if (null == instance) {
                    instance = new GoldApi();
                }
            }
        }
        return instance;
    }

    private GoldApi(){
        Cache cache;
        OkHttpClient okHttpClient = null;
        try {
            File cacheDir = new File(QApplication.getInstance().getCacheDir().getPath(), "AppCache.json");
            cache = new Cache(cacheDir, 10 * 1024 * 1024);

            okHttpClient = new OkHttpClient.Builder()
                    .connectTimeout(15 * 1000, TimeUnit.MILLISECONDS)
                    .readTimeout(20 * 1000, TimeUnit.MILLISECONDS)
                    .writeTimeout(20 * 1000, TimeUnit.MILLISECONDS)
                    .cache(cache)
                    .build();
        } catch (Exception e) {

        }

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constants.GANK_SERVER_IP) //设置Base的访问路径
                .addConverterFactory(GsonConverterFactory.create()) //设置默认的解析库：Gson
                .client(okHttpClient)
                .build();
        mGoldinterface = retrofit.create(GankInterface.class);

        Retrofit meituRetrofit = new Retrofit.Builder()
                .baseUrl(Constants.MEITU_SERVER_IP) //设置Base的访问路径
                .addConverterFactory(GsonConverterFactory.create()) //设置默认的解析库：Gson
                .client(okHttpClient)
                .build();
        mMeituInterface = meituRetrofit.create(MeituInterface.class);
    }

    public static GankInterface getGankApi() {
        return getInstance().mGoldinterface;
    }

    public static MeituInterface getMeituApi() {
        return getInstance().mMeituInterface;
    }

    public interface GankInterface {
        @GET("data/all/{limit}/{page}")
        Call<Results> getGifts(
                @Path("limit") int limit,
                @Path("page") int page
        );

        @GET("data/{dataType}/{limit}/{page}")
        Call<Results> getGiftsByType(
                @Path("dataType") String dataType,
                @Path("limit") int limit,
                @Path("page") int page
        );

        @GET("day/{year}/{month}/{day}")
        Call<Results> getGiftsByDay(
                @Path("year") int year,
                @Path("month") int month,
                @Path("day") int day
        );
    }

    public interface MeituInterface {

        @GET("tnfs/api/list")
        Call<ImageWarper> getImageList(
                @Query("rows") int rows,
                @Query("page") int page
        );

        @GET("tnfs/api/list")
        Call<ImageWarper> getImageListById(
                @Query("rows") int rows,
                @Query("page") int page,
                @Query("id") int id
        );
    }
}
